package cn.yitun.chat.yitunservermanagecore.general.manage.core.controller;

import cn.yitun.chat.yituncommonmessage.ExceptionEnum;
import cn.yitun.chat.yituncommonmessage.YitunException;
import cn.yitun.chat.yituncommonmessage.result.ResultMessage;
import cn.yitun.chat.yitungeneral.annotation.parameter.JsonDefine;
import cn.yitun.chat.yitungeneral.annotation.parameter.RequestParameter;
import cn.yitun.chat.yitunservercommonlib.basebean.web.JwtUserDto;
import cn.yitun.chat.yitunservercommonlib.crud.yitun.BaseController;
import cn.yitun.chat.yitunservercommonlib.dataentity.dto.group.GroupDto;
import cn.yitun.chat.yitunservercommonlib.dataentity.vo.group.GroupVo;
import cn.yitun.chat.yitunservermanagecore.general.bridge.auth.annotation.PermissionMapping;
import cn.yitun.chat.yitunservermanagecore.general.manage.core.service.GroupService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 
 * date 2018-07-19 09:29:16<br>
 * description 群管理
 * @author yitun<br>
 * @since
 */
@RestController
@RequestMapping("/manage/core")
public class GroupController extends BaseController{
	@Resource
	private GroupService groupService;

	@RequestParameter
	@PermissionMapping(name = "群列表", perms = "sys:group:group", type = PermissionMapping.Type.menu)
	@RequestMapping(method = RequestMethod.POST, value = "/group/page")
	public Object list(HttpServletRequest request,
					   @JsonDefine GroupDto groupQuery) {
		ResultMessage rm = new ResultMessage();
		PageHelper.startPage(groupQuery.getCurrentPageNo(), groupQuery.getPageSize());
		List<GroupVo> list = groupService.queryGroupList(groupQuery);
		rm.bodyPut("page", new PageInfo<>(list));
		return rm;
	}

	/**
	 * 保存
	 */
	@PostMapping("/group/save")
	public Object save(JwtUserDto userDto, @JsonDefine GroupDto group){
		setUserMsgSave(group, userDto);
		group.setOwnerNickname(userDto.getNickName());
		if( groupService.save(group) == 0)
			throw new YitunException(ExceptionEnum.ADD_FAIL);

		return new ResultMessage();
	}
	/**
	 * 修改
	 */
	@PostMapping("/group/update")
	public Object update(JwtUserDto userDto, @JsonDefine  GroupDto group){
		setUserMsgUpdate(group, userDto);
		if (group.getId() == null)
			throw new YitunException(ExceptionEnum.LOSE_FILED);

		if( groupService.update(group) == 0 )
			throw new YitunException(ExceptionEnum.EDIT_FAIL);

		return new ResultMessage();
	}

	/**
	 * 删除
	 */
	@DeleteMapping( "/group/remove/{id}")
	public Object remove(@PathVariable("id") Long id){
		if( groupService.remove(id) == 0 )
			throw new YitunException(ExceptionEnum.DELETE_FAILED);

		return new ResultMessage();
	}

	/**
	 * 批量删除
	 */
	@DeleteMapping( "/group/batchRemove/{ids}")
	public Object remove(@PathVariable Long[] ids){
		if( groupService.batchRemove(ids) == 0 )
			throw new YitunException(ExceptionEnum.DELETE_FAILED) ;

		return new ResultMessage();
	}
}
